package com.smart.platform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smart.platform.entity.*;
import com.smart.platform.mapper.ScoreMapper;
import com.smart.platform.service.AnalysisService;
import com.smart.platform.service.ScoreService;
import com.smart.platform.service.StudentsService;
import com.smart.platform.service.TeaClassesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ScoreServiceImpl extends ServiceImpl<ScoreMapper, Score> implements ScoreService {
    @Autowired
    private TeaClassesService teaClassesService;

    @Autowired
    private ScoreMapper scoreMapper;

    @Autowired
    private AnalysisService analysisService;

    @Autowired
    private StudentsService studentsService;

    /**
     * 针对每次科目的单道题添加分数之后 同时计算该同学总分数
     *
     * @param respond
     */
    @Override
    public void update(TextRespond respond) {
        //添加条件构造器 去score表中查询对应学生和考次的数据是否存在
        LambdaQueryWrapper<Score> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Score::getUserId, respond.getUserId())
                .eq(Score::getState, respond.getState());

        //获取传递过来的修改的考试科目
        Integer course = respond.getCourse();

        Score score = super.getOne(lambdaQueryWrapper);


        if (score != null) {
            switch (course) {
                case 1:
                    score.setChinese(score.getChinese() + respond.getScore());
                    break;
                case 2:
                    score.setMath(score.getMath() + respond.getScore());
                    break;
                case 3:
                    score.setEnglish(score.getEnglish() + respond.getScore());
                    break;
                case 4:
                    score.setPhysics(score.getPhysics() + respond.getScore());
                    break;
                case 5:
                    score.setChemistry(score.getChemistry() + respond.getScore());
                    break;
                case 6:
                    score.setBiology(score.getBiology() + respond.getScore());
                    break;
                case 7:
                    score.setPolitical(score.getPolitical() + respond.getScore());
                    break;
                case 8:
                    score.setHistory(score.getHistory() + respond.getScore());
                    break;
                case 9:
                    score.setGeography(score.getGeography() + respond.getScore());
                    break;
            }

            score.setTotal(score.getTotal() + respond.getScore());

            super.update(score, lambdaQueryWrapper);
        } else { //此时代表score表中没有该学生对应考次的数据 所以此时需要插入该条数据
            LambdaQueryWrapper<Students> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Students::getUserId, respond.getUserId());
            Students student = studentsService.getOne(queryWrapper);

            Score newScore = new Score();
            newScore.setUserId(respond.getUserId());
            newScore.setName(student.getName());
            newScore.setClasses(student.getClasses());
            newScore.setGrade(student.getGrade());
            newScore.setState((respond.getState()));

            switch (course) {
                case 1:
                    newScore.setChinese(Double.valueOf(respond.getScore()));
                    break;
                case 2:
                    newScore.setMath(Double.valueOf(respond.getScore()));
                    break;
                case 3:
                    newScore.setEnglish(Double.valueOf(respond.getScore()));
                    break;
                case 4:
                    newScore.setPhysics(Double.valueOf(respond.getScore()));
                    break;
                case 5:
                    newScore.setChemistry(Double.valueOf(respond.getScore()));
                    break;
                case 6:
                    newScore.setBiology(Double.valueOf(respond.getScore()));
                    break;
                case 7:
                    newScore.setPolitical(Double.valueOf(respond.getScore()));
                    break;
                case 8:
                    newScore.setHistory(Double.valueOf(respond.getScore()));
                    break;
                case 9:
                    newScore.setGeography(Double.valueOf(respond.getScore()));
                    break;

            }

            //直接设置总分值为此时的单科目成绩
            newScore.setTotal(Double.valueOf(respond.getScore()));

            super.save(newScore);
        }

    }

    @Override
    public Analysis save(String userId, Integer state) {
        LambdaQueryWrapper<TeaClasses> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TeaClasses::getUserId, userId);
        //userId为教师工号 获取该教师所管理的班级信息
        TeaClasses one = teaClassesService.getOne(queryWrapper);

        int classes = one.getClasses();
        int grade = one.getGrade();

        LambdaQueryWrapper<Analysis> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper.eq(Analysis::getClasses, classes)
                .eq(Analysis::getGrade, grade);

        Analysis analysisServiceOne = analysisService.getOne(lambdaQueryWrapper);
        Long id = null;

        if (analysisServiceOne != null) {
            id = analysisServiceOne.getId();
        }
        //有数据就跟新然后再返回
        analysisServiceOne = extracted(state, classes, grade);

        analysisServiceOne.setId(id);
        analysisServiceOne.setGrade(one.getGrade());
        analysisServiceOne.setClasses(one.getClasses());
        analysisServiceOne.setState(state);

        //有id就会根据id更新 无id就会新增
        analysisService.saveOrUpdate(analysisServiceOne);

        return analysisService.getOne(lambdaQueryWrapper);
    }

    private Analysis extracted(Integer state, int classes, int grade) {
        Analysis analysis = new Analysis();


        float totalAvg = scoreMapper.selectTotalAvg(classes, grade, state);
        float totalMax = scoreMapper.selectTotalMax(classes, grade, state);
        float totalMin = scoreMapper.selectTotalMin(classes, grade, state);
        float chineseAvg = scoreMapper.selectChineseAvg(classes, grade, state);
        float mathAvg = scoreMapper.selectMathAvg(classes, grade, state);
        float englishAvg = scoreMapper.selectEnglishAvg(classes, grade, state);
        float physicsAvg = scoreMapper.selectPhysicsAvg(classes, grade, state);
        float chemistryAvg = scoreMapper.selectChemistryAvg(classes, grade, state);
        float biologyAvg = scoreMapper.selectBiologyAvg(classes, grade, state);
        float politicalAvg = scoreMapper.selectPoliticalAvg(classes, grade, state);
        float historyAvg = scoreMapper.selectHistoryAvg(classes, grade, state);
        float geographyAvg = scoreMapper.selectGeographyAvg(classes, grade, state);

        analysis.setAverageTotal(totalAvg);
        analysis.setMaxTotal(totalMax);
        analysis.setMinTotal(totalMin);
        analysis.setChineseAverage(chineseAvg);
        analysis.setMathAverage(mathAvg);
        analysis.setEnglishAverage(englishAvg);
        analysis.setPhysicsAverage(physicsAvg);
        analysis.setChemistryAverage(chemistryAvg);
        analysis.setBiologyAverage(biologyAvg);
        analysis.setPoliticalAverage(politicalAvg);
        analysis.setHistoryAverage(historyAvg);
        analysis.setGeographyAverage(geographyAvg);

        return analysis;
    }

}
